package com.pengxun.manager.service.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomStringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.pengxun.manager.dao.system.CompanyDao;
import com.pengxun.manager.dao.system.UserinfoDao;
import com.pengxun.manager.enums.CompanyStatusEnum;
import com.pengxun.manager.enums.UserTypeEnums;
import com.pengxun.manager.model.system.Company;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.CompanyService;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.vo.CompanyStructureVO;
import com.pengxun.manager.vo.CompanyVO;
import com.pengxun.manager.vo.DepartmentStructureVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.utils.ValidationUtils;


@Service
public class CompanyServiceImpl implements CompanyService {


    @Resource
    private CompanyDao companyDao;

    @Resource
    private UserinfoDao userinfoDao;

    @Resource
    private LoginService loginService;

    @Override
    public int insert(Company company) {

        ValidationUtils.validate(company);
        Userinfo user = loginService.getCurrentUser();
        //生成公司代码
        company.setCompanyCode(generateCompanyCode());

        company.setCreateDate(new Date());
        company.setCreateBy(user.getUserId());
        company.setUpdateDate(new Date());
        company.setUpdateBy(user.getUserId());
        if (company.getStatus() == null) {
            company.setStatus(CompanyStatusEnum.USABLE.getStatus());
        }
        return companyDao.insert(company);
    }

    @Override
    public int update(Company company) {
        ValidationUtils.validate(company);
        Userinfo user = loginService.getCurrentUser();

        company.setUpdateDate(new Date());
        company.setUpdateBy(user.getUserId());

        //删除cdn文件
        return companyDao.update(company);
    }

    @Override
    public int updateStatus(Company company) {
        Userinfo user = loginService.getCurrentUser();
        company.setUpdateDate(new Date());
        company.setUpdateBy(user.getUserId());
        //删除cdn文件
        return companyDao.update(company);
    }

    @Override
    public int delete(Integer companyId) {

        //查询是否还有用户
        int count = userinfoDao.countByCompanyId(companyId);
        if (count > 0) {
            throw new EastrocException("该公司还有用户，不能删除");
        }

        Company company = new Company();
        company.setCompanyId(companyId);
        //软删除 -1
        company.setStatus(CompanyStatusEnum.DELETE.getStatus());
        return companyDao.update(company);
    }

    @Override
    public Page findByPage(CompanyVO companyVO) {

        Userinfo user = loginService.getCurrentUser();


//        List<Company> companys = companyDao.findByPage(companyVO);

        List<Company> companys = new ArrayList<>();
        if (user.getUserType().equals(UserTypeEnums.PLATFORM_ADMIN.getType())) {
            companys.addAll(companyDao.findByPage(companyVO));
        }else{
            companys.add(companyDao.findByCompanyId(user.getCompanyId()));
        }

        companyVO.setData(companys);
        return (Page) companyVO;
    }

    @Override
    public Company findByCompanyId(Integer companyId) {
        return companyDao.findByCompanyId(companyId);
    }

    @Override
    public List<Company> findAll() {
        Userinfo user = loginService.getCurrentUser();

        List<Company> list = new ArrayList<>();
        if (user.getUserType().equals(UserTypeEnums.PLATFORM_ADMIN.getType())) {
            list.addAll(companyDao.findAll());
        }else{
            list.add(companyDao.findByCompanyId(user.getCompanyId()));
        }
        return list;
    }

    public String generateCompanyCode() {


        return DateTime.now().toString("yyyyMMddHHmmss")+RandomStringUtils.randomNumeric(4);
    }


    /**
     * 返回组织架构
     *
     * @return
     */

    public List<CompanyStructureVO> getCompanyStructure() {
        List<CompanyStructureVO> companyStructureVOList = new ArrayList<>();

        List<Company> companyList = findAll();
        if (companyList == null) {
            return companyStructureVOList;
        }

        List<Integer> companyIds = new ArrayList<>();
        for (Company company : companyList) {
            companyIds.add(company.getCompanyId());
        }

        for (Company company : companyList) {

            CompanyStructureVO companyStructureVO = new CompanyStructureVO();
            BeanUtils.copyProperties(company, companyStructureVO);

            List<DepartmentStructureVO> children = new ArrayList<>();
            children.sort((s1, s2) -> s1.getSort().compareTo(s2.getSort()));
            companyStructureVO.setChildren(children);
            companyStructureVOList.add(companyStructureVO);
        }
        return companyStructureVOList;
    }



}
